<template>
  <page-view class="page-view" :title="false">
    <div slot="headerContent">
      <a-breadcrumb class="s-breadcrumb">
        <a-breadcrumb-item>资产管理</a-breadcrumb-item>
        <a-breadcrumb-item><a @click="goback">物品管理</a></a-breadcrumb-item>
        <a-breadcrumb-item>新增物品</a-breadcrumb-item>
      </a-breadcrumb>
    </div>

    <a-card class="s-card">
      <a-spin :spinning="sending">
        <add-item
          show-button
          :form-item-layout="formItemLayout"
          :sending="sending"
          :item-data="itemData"
          @sendingChange="sendingChange"
          @submit="handleSubmit"
          @cancel="goback"
        />
      </a-spin>
    </a-card>
  </page-view>
</template>

<script>
import { PageView, AddItem } from '@/components'

export default {
  name: 'Add',
  components: { PageView, AddItem },
  inject: ['formItemLayout'],

  async fetch ({ store, app }) {
    const { $get } = store
    store.dispatch('property/getItemTypes')
    const list = []
    if (!$get(store, 'state.basic.deptList.length')) list.push({ fn: app.$departmentListFunc })
    if (!$get(store, 'state.basic.areaList.length')) list.push({ fn: app.$areaListFunc })
    await app.$requestList(list)
    store.dispatch('basic/setDeptArea')
  },

  data () {
    return {
      sending: false,

      itemData: {}
    }
  },

  beforeCreate () {
    sessionStorage.removeItem('temp_new_item_record')
  },

  methods: {
    sendingChange (v) {
      this.$toggleSending(v)
    },

    /**
     * 提交数据
     */
    async handleSubmit (param = {}) {
      console.log('param', param)
      this.$toggleSending(1)
      const { $axios, $message, $get, goback } = this
      const res = await $axios.post('api/assetsGoods/', param).then(res => res).catch(e => false)
      this.$toggleSending()
      $message.destroy()
      if ($get(res, 'data.code') !== '000000') return $message.error($get(res, 'data.mesg', '请稍后再试'))
      sessionStorage.removeItem('temp_new_item_record')
      $message.info('新增成功')
      goback()
    },

    goback () {
      this.$router.back()
      sessionStorage.removeItem('temp_new_item_record')
    }
  }
}
</script>
